home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / Namespace.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  1.3 KB  |  56 lines

  1. from rdflib.URIRef import URIRef
  2.  
  3. import logging
  4.  
  5. _logger = logging.getLogger(__name__)
  6.  
  7.  
  8. class Namespace(URIRef):
  9.  
  10.     def term(self, name):
  11.         return URIRef(self + name)
  12.  
  13.     def __getitem__(self, key, default=None):
  14.         return self.term(key)
  15.  
  16.     def __getattr__(self, name):
  17.         if name.startswith("__"): # ignore any special Python names!
  18.             raise AttributeError
  19.         else:
  20.             return self.term(name)
  21.  
  22.  
  23. class NamespaceDict(dict):
  24.  
  25.     def __new__(cls, uri=None, context=None):
  26.         inst = dict.__new__(cls)
  27.         inst.uri = uri # TODO: do we need to set these both here and in __init__ ??
  28.         inst.__context = context
  29.         return inst
  30.  
  31.     def __init__(self, uri, context=None):
  32.         self.uri = uri
  33.         self.__context = context
  34.  
  35.     def term(self, name):
  36.         uri = self.get(name)
  37.         if uri is None:
  38.             uri = URIRef(self.uri + name)
  39.             if self.__context and (uri, None, None) not in self.__context:
  40.                 _logger.warning("%s not defined" % uri)
  41.             self[name] = uri
  42.         return uri 
  43.  
  44.     def __getattr__(self, name):
  45.         return self.term(name)
  46.  
  47.     def __getitem__(self, key, default=None):
  48.         return self.term(key) or default
  49.  
  50.     def __str__(self):
  51.         return self.uri
  52.  
  53.     def __repr__(self):
  54.         return """rdflib.NamespaceDict('%s')""" % str(self.uri)
  55.  
  56.